<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Fgroup as FgroupModel;
use app\common\model\Flike as FlikeModel;
use app\common\model\Friends as FriendsModel;
use app\common\model\Member as MemberModel;

/**
 * 论坛控制器
 * Class Fgroup
 * @package app\home\controller
 */
class Fgroup extends Base
{
    /**
     * [getIndex  论坛列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $model = new FgroupModel();
        $fmodel = new FlikeModel();
        $frmodel = new FriendsModel();
        $this->getPageAndSize($param);
        $mem = $this->getMember($param['token']);
        $map = [];
        $map1 = [];
        $map2 = [];

        try{
            $map['parid'] = 0;
            if(!empty($param['memid'])){
                $map['memid'] = $param['memid'];
            }
            $count = $model->getCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getByConditon($map,$this->from, $this->size);

            foreach($lists as $k=>$v){
                $http = substr($v['img'],0,4);
                if($http != 'http') {
                    $lists[$k]['img'] = config('group_domain_name') . $v['img'];
                }

                if(!empty($v['imgs'])){
                    $img = explode(',',$v['imgs']);
                    foreach($img as $k1=>$v1){
                        if(empty($v1)){
                            unset($img[$k1]);
                        }else{
                            $http = substr($v1,0,4);
                            if($http != 'http') {
                                $img[$k1] = config('group_domain_name') . $v1;
                            }
                        }
                    }
                    $lists[$k]['imgs'] = $img;
                }

                $flike_num= $fmodel->getAllCount(['fgroup_id'=>$v['id']]);
                $lists[$k]['flike_num'] = $flike_num;

                $likedata = $fmodel->findOne(['fgroup_id'=>$v['id'],'memid'=>$mem['id']]);
                if(empty($likedata)){
                    $lists[$k]['is_like'] = 0;
                }else{
                    $lists[$k]['is_like'] = 1;
                }

                $pinlun_num = $model->getAllCount(['parid'=>$v['id']]);
                $lists[$k]['pinlun_num'] = $pinlun_num;

                $lists[$k]['addtime'] = get_last_time($v['addtime']);

                $map1['unino'] = '_'.$mem['id'].'_'.$v['memid'].'_';
                $map2['unino'] = '_'.$v['memid'].'_'.$mem['id'].'_';
                $friend = $frmodel->getOne($map1,$map2);

                if(!empty($friend)){
                    $lists[$k]['is_focus'] = 1;
                }else{
                    $lists[$k]['is_focus'] = 0;
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }



    /**
     * [getOne  根据论坛id获取内容]
     * @param
     * @return json
     * @author [C。]
     */
    public function getOne()
    {
        $param = input('param.');
        $model = new FgroupModel();
        $mmodel = new MemberModel();
        $frmodel = new FriendsModel();
        $fmodel = new FlikeModel();
        $mem = $this->getMember($param['token']);
        try{
            $data = $model->findData($param['id'],'id');
            $member = $mmodel->findData($data['memid'],'id','username,img');
            $http = substr($member['img'],0,4);
            if($http != 'http')
                $data['img'] = config('domain_name').$member['img'];
            else
                $data['img'] = $member['img'];
            $data['username'] = $member['username'];

            if(!empty($data['imgs'])){
                $img = explode(',',$data['imgs']);
                foreach($img as $k1=>$v1){
                    if(empty($v1)){
                        unset($img[$k1]);
                    }else{
                        $http = substr($v1,0,4);
                        if($http != 'http') {
                            $img[$k1] = config('group_domain_name') . $v1;
                        }
                    }
                }
                $data['imgs'] = $img;
            }

            $map1['unino'] = '_'.$mem['id'].'_'.$data['memid'].'_';
            $map2['unino'] = '_'.$data['memid'].'_'.$mem['id'].'_';
            $friend = $frmodel->getOne($map1,$map2);

            if(!empty($friend)){
                $data['is_focus'] = 1;
            }else{
                $data['is_focus'] = 0;
            }
            $data['addtime'] = get_last_time($data['addtime']);

            $likedata = $fmodel->findOne(['fgroup_id'=>$data['id'],'memid'=>$mem['id']]);
            if(empty($likedata)){
                $data['is_like'] = 0;
            }else{
                $data['is_like'] = 1;
            }

            $flike_num= $fmodel->getAllCount(['fgroup_id'=>$data['id']]);
            $data['flike_num'] = $flike_num;
            $pinlun_num = $model->getAllCount(['parid'=>$data['id']]);
            $data['pinlun_num'] = $pinlun_num;
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [getReply  根据评论id获取回复]
     * @param
     * @return json
     * @author [C。]
     */
    public function getReply()
    {
        $param = input('param.');
        $model = new FgroupModel();
        $mmodel = new MemberModel();
        $fmodel = new FlikeModel();
        $member = $this->getMember($param['token']);
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['parid'] = $param['id'];
            $count = $model->getCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $data = $model->getByConditon($map,$this->from, $this->size);
            foreach($data as $k=>$v){
                $mem = $mmodel->findData($v['memid'],'id','img,username,identity');
                $http = substr($mem['img'],0,4);
                if($http != 'http')
                    $data[$k]['img'] = config('domain_name').$mem['img'];
                else
                    $data[$k]['img'] = $mem['img'];
                $data[$k]['username'] = $mem['username'];
                $data[$k]['identity'] = $mem['identity'];

                $flike_num= $fmodel->getAllCount(['fgroup_id'=>$v['id']]);
                $data[$k]['flike_num'] = $flike_num;

                $data[$k]['addtime'] = get_last_time($v['addtime']);

                $num = $model->getCountByCondition(['parid'=>$v['id']]);
                $result = $model->getByConditon(['parid'=>$v['id']],0,2);

                $likedata = $fmodel->findOne(['fgroup_id'=>$v['id'],'memid'=>$member['id']]);
                if(empty($likedata)){
                    $data[$k]['is_like'] = 0;
                }else{
                    $data[$k]['is_like'] = 1;
                }

                foreach($result as $k1=>$v1){
                    $http = substr($v1['img'],0,4);
                    if($http != 'http')
                        $result[$k1]['img'] = config('domain_name').$v1['img'];
                }
                $data[$k]['total'] = $num;
                $data[$k]['lv'] = $result;
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $data,  //列表数据
        ]);
    }
    /**
     * [getReplyson  根据评论id获取子回复]
     * @param
     * @return json
     * @author [C。]
     */
    public function getReplyson()
    {
        $param = input('param.');
        $model = new FgroupModel();
        $mmodel = new MemberModel();
        $fmodel = new FlikeModel();
        $this->getPageAndSize($param);
        $member = $this->getMember($param['token']);
        $map = [];
        try{
            $map['parid'] = $param['id'];
            $count = $model->getCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getByConditon($map,$this->from, $this->size);

            foreach($lists as $k=>$v) {
                $mem = $mmodel->findData($v['memid'], 'id', 'img,username,identity');
                $http = substr($mem['img'],0,4);
                if($http != 'http')
                    $lists[$k]['img'] = config('domain_name').$mem['img'];
                $lists[$k]['username'] = $mem['username'];
                if(!empty($v['touserid'])){
                    $name = $mmodel->findData($v['touserid'], 'id', 'username');
                    $lists[$k]['tousername'] = $name['username'];
                }

                $lists[$k]['addtime'] = get_last_time($v['addtime']);

                $likedata = $fmodel->findOne(['fgroup_id'=>$v['id'],'memid'=>$member['id']]);
                if(empty($likedata)){
                    $lists[$k]['is_like'] = 0;
                }else{
                    $lists[$k]['is_like'] = 1;
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getReplyson  根据评论id获取子回复]
     * @param
     * @return json
     * @author [C。]
     */
    public function getDynamic()
    {
        $param = input('param.');
        $model = new FgroupModel();
        $this->getPageAndSize($param);
        $member = $this->getMember($param['token']);
        try{
            $map['touserid'] = $member['id'];
            $map['touserid'] = $member['id'];
            $count = $model->getCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getByConditon($map,$this->from, $this->size);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postDynamic  发布动态]
     * @param
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $model = new FgroupModel();
        $member = $this->getMember($param['token']);
        try{
            $data = array(
                'memid'=>$member['id'],
                'content'=>$param['content'],
                'addtime'=>time(),
            );
            if(!empty($param['topid'])){
                $data['topid'] = $param['topid'];
            }
            if(!empty($param['touserid'])){
                $data['touserid'] = $param['touserid'];
            }
            if(!empty($param['parid'])){
                $data['parid'] = $param['parid'];
            }
            if(!empty($param['imgs'])){
                $data['imgs'] = $param['imgs'];
            }
            if(!empty($param['addr'])){
                $data['addr'] = $param['addr'];
            }
            $model->addData($data);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
}